package com.javafactroy.kunlong.test.db;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javafactroy.kunlong.DbAgent;
import com.javafactroy.kunlong.base.config.datasource.JdbcTemplateFactroy;
import com.javafactroy.kunlong.base.metadata.model.TMetadataModel;
import com.javafactroy.kunlong.base.metadata.valueobj.MetadataTable;
import com.javafactroy.kunlong.base.model.ReturnPageInfo;
import com.javafactroy.kunlong.base.service.MyService;
import com.javafactroy.kunlong.data.dao.oracle.OracleColumnsMapper;
import com.javafactroy.kunlong.data.dao.oracle.OracleTablesMapper;
import com.javafactroy.kunlong.data.entity.mysql.MysqlColumns;
import com.javafactroy.kunlong.codefactroy.iface.ICodeTemplate;
import com.javafactroy.kunlong.codefactroy.iface.IStringTemplate;
import com.javafactroy.kunlong.codefactroy.service.codetemplate.CodeTemplateFactroy;
import com.javafactroy.kunlong.codefactroy.service.base.FileTemplateFactroy;
import com.javafactroy.kunlong.codefactroy.service.filetemplate.db.MapperFileTemplate;
import com.javafactroy.kunlong.codefactroy.service.stringtemplate.StringTemplate;
import com.javafactroy.kunlong.data.dao.mysql.GeneralMysqlSqlMapper;
import com.javafactroy.kunlong.data.dao.mysql.MysqlColumnsMapper;
import com.javafactroy.kunlong.data.dao.mysql.TestMapper;
import com.javafactroy.kunlong.data.dao.oracle.OracleSqlMapper;
import com.javafactroy.kunlong.data.entity.Job;
import com.javafactroy.kunlong.data.entity.TaskLogEntity;
import com.javafactroy.kunlong.data.entity.oracle.OracleColComment;
import com.javafactroy.kunlong.data.entity.oracle.OracleColumns;
import com.javafactroy.kunlong.data.entity.oracle.OracleTables;
import com.javafactroy.kunlong.common.utils.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.core.*;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import tk.mybatis.mapper.entity.Example;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

@SpringBootTest(classes = {DbAgent.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ExtendWith(SpringExtension.class)
@Slf4j
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@TestMethodOrder(MethodOrderer.MethodName.class)

public class TestOracle {
    @Autowired
    MysqlColumnsMapper mysqlColumnsMapper;

    @Autowired
    TestMapper testMapper;

    @Autowired
    TMetadataModel metadataModel;

    @Autowired
    GeneralMysqlSqlMapper generalSqlMapper;

    @Autowired
    List<ApplicationContext> list;

    @Autowired
    CodeTemplateFactroy codeTemplateFactroy;
    @Autowired
    FileTemplateFactroy fileTemplateFactroy;

    // @Autowired
    //JobRunner jobRunner;


    @Test
    public void test001() throws Exception {

        //   jobRunner.run();
    }


    @Test
    public void test003_selectSql() {

        //  PageHelper.startPage(1, 2);
        List ret = metadataModel.selectMysqlColumns("employee");
        log.info("t = {}", CommonUtils.toJSONString(ret));
        log.info("size={}", ret.size());
        long c = mysqlColumnsMapper.selectCount(new MysqlColumns());
        log.info("c = {}", c);
    }

    @Test
    public void test004_selectSql() {

        PageHelper.startPage(1, 11);

        List<TaskLogEntity> t = generalSqlMapper.select(new TaskLogEntity().setOprtType(1));
        log.info("t = {}", CommonUtils.toJSONString(t));
        PageInfo<TaskLogEntity> pageInfo = new PageInfo<>(t);

        long total = pageInfo.getTotal();
        log.info("size={},total={}", t.size(), total);

    }

    //@Autowired
    //SelectModel<TaskLogMap,GeneralSqlMapper> selectModel;
    @Test
    public void test005_selectSql() {

        PageHelper.startPage(1, 1);

        List<TaskLogEntity> t = generalSqlMapper.select(new TaskLogEntity().setOprtType(1));
        log.info("t = {}", CommonUtils.toJSONString(t));
        ReturnPageInfo<TaskLogEntity> apiPageInfo = new ReturnPageInfo<>();
        apiPageInfo.valueOf(t);
        log.info("size={}, total={}", t.size(), apiPageInfo.getTotal());

    }

    public static void main(String[] args) {
        ICodeTemplate i = () -> {
            log.info("111");
            return "";
        };

        IStringTemplate.test();
        Function<String, String> f = a -> a;
        log.info("{}", f.apply("111"));

    }

    @Autowired
    MyService myService;

    //TMetadataModel
    @Test
    public void test006_findMeta() {

        MetadataTable tmetadataTabl = metadataModel.findMetadata("tasklog");
        log.info("tmetadataTabl={}", CommonUtils.toJSONString(tmetadataTabl));
        StrUtil.repeat("1", 299);
        myService.test(new TaskLogEntity().setUser(StrUtil.repeat("中", 90)));
        //log.info(StrUtil.toCamelCase("v_ddd"));
    }

    @Autowired
    StringTemplate stringTemplate;

    @Autowired
    MapperFileTemplate fileTemplate;


    @Autowired
    OracleSqlMapper generalOracleSqlMapper;

    @Autowired
    List<JdbcTemplate> jdbcTemplates;

    @Test
    public void test009_oracl() {
        PageHelper.startPage(1, 3);
        List list = generalOracleSqlMapper.generalSelect("select * from jobs");
        log.info("oralce jobs  =  {}", CommonUtils.toJSONString(list));

    }

    @Autowired
    JdbcTemplateFactroy jdbcTemplateFactroy;


    public static class MyMapper implements RowMapper<Job> {

        @Override
        public Job mapRow(ResultSet rs, int rowNum) throws SQLException {
            Job job = new Job().setJobId(rs.getString("JOB_ID"));
            rs.getString("JOB_ID");
            return job;
        }
    }

    @Test
    public void test010_oraclGeneral() {

        List<Map<String, Object>> jobs = jdbcTemplateFactroy.findOracle().queryForList("select * from jobs");
        log.info("jobs = {}", CommonUtils.toJSONString(jobs));

    }

    @Test
    public void test011_oraclObject() {

        PageHelper.startPage(1, 2);
        List<Job> jobs = jdbcTemplateFactroy.findOracle().query("select * from jobs ", new BeanPropertyRowMapper(Job.class));
        log.info("jobs = {}", CommonUtils.toJSONString(jobs));

    }

    @Test
    public void test012_oraclProc() {
        List<SqlParameter> parameters = new ArrayList<>();
        Map<String, Object> jobs = jdbcTemplateFactroy.findOracle().call(new CallableStatementCreator() {
            @Override
            public CallableStatement createCallableStatement(Connection con) throws SQLException {
                String sql = "{CALL LEIJMDAS.MYSQL(?)}";
                CallableStatement cs = con.prepareCall(sql);
                // cs.setString(1, "outparam");
                cs.registerOutParameter(1, JDBCType.DECIMAL);
                // 设置存储过程的输入参数
                return cs;
            }
        }, parameters);
        log.info("parameters={}", jobs);
    }

    @Test
    public void test013_callOoraclProc() {
        String lines = "1| |3|";
        List<String> list = StrUtil.split(lines, "|", 100, true, false);
        log.info("list = {}", list);
    }

    @Test
    public void test014_selectByExample() {
        Example example = new Example(TaskLogEntity.class);
        example.createCriteria().andLessThan("id", 10);
        List<TaskLogEntity> taskLogEntities = generalOracleSqlMapper.selectByExample(example);
        log.info("task={}", taskLogEntities);

    }

    @Test
    public void test015_col_comment() {
        List<OracleColComment> list = generalOracleSqlMapper.generalSelectComments("SELECT TABLE_NAME ,column_name,  comments " +
                "FROM user_col_comments");
        log.info("list={}", list);
    }
    //SELECT *   FROM user_tables

    @Test
    public void test016_table() {
        List<Map> list = generalOracleSqlMapper.generalSelect("SELECT * FROM user_tables");
        log.info("list={}", list);
    }

    //select * from USER_TAB_COLS
    @Test
    public void test017_table() {
        List<Map> list = generalOracleSqlMapper.generalSelect("SELECT * FROM USER_TAB_COLS");
        log.info("list={}", list);
    }

    @Test
    public void test018_tables() {
        List<OracleTables> list = generalOracleSqlMapper.generalSelectTables("SELECT * FROM user_tables");
        log.info("list={}", list);
    }

    @Autowired
    OracleTablesMapper oracleTablesMapper;
    @Autowired
    OracleColumnsMapper oracleColumnsMapper;

    @Test
    public void test019_ts() {
        List<OracleTables> list = oracleTablesMapper.selectAll();
        log.info("list={}", list);
    }


    @Test
    public void test020_cols() {
        PageHelper.startPage(1,2);
        List<OracleColumns> list = oracleColumnsMapper.select(new OracleColumns());
        log.info("list={}", CommonUtils.toJSONString(list));
    }


}
